/*
 * License GNU LGPL
 * Copyright (C) 2012 Amrullah <amrullah@panemu.com>.
 */
package com.abc.cheque.ui.table;


import java.util.ArrayList;
import java.util.List;

import javafx.scene.Node;
import javafx.scene.control.Control;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TextField;
import javafx.util.Callback;

import com.abc.cheque.common.TableCriteria.Operator;

/**
 *
 * @author amrullah
 */
public class TextColumn<S extends Object> extends BaseColumn<S, String> {

    private TextField searchInputControl = new TextField();
    private SearchMenuItemBase<String> searchMenuItem = new SearchMenuItemBase<String>(this) {
        @Override
        protected Node getInputControl() {
            searchInputControl.setPromptText("kata kunci");
            return searchInputControl;
        }

        @Override
        protected List<Operator> getOperators() {
            List<Operator> lst = new ArrayList<>();
            lst.add(Operator.eq);
            lst.add(Operator.ilike_begin);
            lst.add(Operator.ilike_anywhere);
            lst.add(Operator.ilike_end);
            lst.add(Operator.is_null);
            lst.add(Operator.is_not_null);
            return lst;
        }

        @Override
        protected String getValue() {
            return searchInputControl.getText();
        }
    };

    public TextColumn(String propertyName) {
        this(propertyName, 100);
    }

    public TextColumn(String propertyName, double preferredWidth) {
        super(propertyName, preferredWidth);

        Callback<TableColumn<S, String>, TableCell<S, String>> cellFactory =
                new Callback<TableColumn<S, String>, TableCell<S, String>>() {
                    @Override
                    public TableCell call(TableColumn p) {
                        return new TextTableCell();
                    }
                };

        setCellFactory(cellFactory);
    }

    @Override
    MenuItem getSearchMenuItem() {
        searchInputControl.setText(getDefaultSearchValue());
        return searchMenuItem;
    }

    public class TextTableCell<S> extends BaseCell<S, String> {

        private TextField textField = new TextField();

        public TextTableCell() {
            super();
            this.setAlignment(TextColumn.this.getAlignment());
        }
        
        @Override
        protected void setValueToEditor(String value) {
            textField.setText(value);
        }

        @Override
        protected String getValueFromEditor() {
            return textField.getText();
        }

        @Override
        protected Control getEditor() {
            if (textField == null) {
                textField = new TextField();
                textField.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);
            }
            return textField;
        }
    }

}
